package com.pei.util;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.Statement;

public class BaseDao {
	static String url="jdbc:mysql://127.0.0.1:3306/purifier?useUnicode=true&characterEncoding=UTF-8";
	static String user="root";
	static String pwd="123";
	static String jdbc="com.mysql.jdbc.Driver";
	static{
		try {
			Class.forName(jdbc);
		} catch (ClassNotFoundException e) {
			System.out.println("数据库驱动失败");
		}
	}
	public static Connection getConnection(){
		Connection conn=null;
		try {
			conn=DriverManager.getConnection(url,user,pwd);
		} catch (Exception e) {
			System.out.println("数据库连接失败");
		}
		return conn;
	}
	public static void closeConnection(Connection conn,Statement stat,ResultSet rs){
		try {
			if(rs!=null)
				rs.close();
			if(stat!=null)
				stat.close();
			if(conn!=null)
				conn.close();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}
	}
	public static int executeUpdate(Object[] ob,String sql){
		int j=0;
		Connection conn=null;
		PreparedStatement stat=null;
		try {
			conn=BaseDao.getConnection();
			stat=conn.prepareStatement(sql);
			for (int i = 0; i < ob.length; i++) {
				stat.setObject(i+1, ob[i]);
			}
			j=stat.executeUpdate();
		} catch (Exception e) {
			System.out.println(e.getMessage());
		}finally{
			BaseDao.closeConnection(conn, stat, null);
		}
		return j;
	}
}
